namespace devtm.Aop.Emit
{
    using System;
    using Mono.Cecil;
    using devtm.Aop.Emit;
    using Mono.Cecil.Cil;
    

    [Serializable]
    public class EmitCastExpression : EmitExpression
    {
        private EmitExpression expression;
        private EmitTypeReference targetType;


        public EmitCastExpression()
        {
        }


        public EmitCastExpression(EmitTypeReference targetType, EmitExpression expression)
        {
            this.TargetType = targetType;
            this.Expression = expression;
        }


        public EmitCastExpression(TypeReference targetType, EmitExpression expression)
        {
            this.TargetType = new EmitTypeReference(targetType);
            this.Expression = expression;
        }

        public EmitExpression Expression
        {
            get
            {
                return this.expression;
            }
            set
            {
                this.expression = value;
            }
        }

        public EmitTypeReference TargetType
        {
            get
            {
                if (this.targetType == null)
                {
                    this.targetType = new EmitTypeReference();
                }
                return this.targetType;
            }
            set
            {
                this.targetType = value;
            }
        }



        public override void Compile(CilWorker gen)
        {

            this.expression.UserData.Add("way", "get");
            this.expression.Compile(gen);
            this.expression.UserData.Remove("way");
            

            gen.Insert(OpCodes.Isinst, this.targetType.Type);
        }


    }
}
